//
//  OrganDetailViewController.m
//  SmartWatch
//
//  Created by zwj on 2022/11/26.
//

#import "OrganDetailViewController.h"
#import "YDCircleProgressView.h"
#import "OrganDetailView.h"
#import "OrganDetailSingleViewController.h"

@interface OrganDetailViewController ()
@property(nonatomic,strong) NSArray *dataArray;
@end

@implementation OrganDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isHideNavigationBar = YES;
    [self.view az_setGradientBackgroundWithColors:@[[UIColor colorWithRGB:0xC4EFEC alpha:1.0],[UIColor colorWithRGB:0xC4EFEC alpha:0.75],[UIColor colorWithRGB:0xC4EFEC alpha:0.1]] locations:@[@0.0,@0.3,@1.0] startPoint:CGPointMake(0.5, 0) endPoint:CGPointMake(0.5, 1.0)];
    
    MyLinearLayout *navView = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
    navView.myHorzMargin = 0;
    navView.gravity = MyGravity_Vert_Center | MyGravity_Horz_Between;
    navView.myHeight = 44;
    navView.myTop = StatusBarHeight;
    navView.padding = UIEdgeInsetsMake(0, 15, 0, 15);
    [self.view addSubview:navView];
    
    {
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [backBtn setImage:UIImageMake(@"icon_arrow_left") forState:UIControlStateNormal];
        backBtn.mySize = CGSizeMake( 40, 40);
        backBtn.imageEdgeInsets = UIEdgeInsetsMake(10,10,10,10);
        [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        [navView addSubview:backBtn];
        
        UILabel *content = [UILabel createLabelText:@"器官能量" font:UIFontBoldMake(17) textColor:COLOR1515];
        content.mySize = kAutoSize;
        [navView addSubview:content];
        
        UIButton *right = [UIButton createButtonText :@"     " font:UIFontMake(14) textColor:COLOR1515];
        right.mySize = kAutoSize;
        [navView addSubview:right];
        
    }
}


- (void)initSubviews {
    [super initSubviews];
    
    CGFloat topHeight = NavigationContentTop + (kScreenHeight - NavigationContentTop - 590)/2.0;
    
    UIImageView *backImage = [[UIImageView alloc]initWithImage:UIImageMake(@"monitor_quanshen")];
    backImage.frame = CGRectMake((kScreenWidth - 245)/2.0 , topHeight, 245, 590);
    [self.view addSubview:backImage];
    
    MyLinearLayout *root = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
    root.myHorzMargin = 15;
    root.myTop = topHeight - 30;
    root.myHeight = 590 + 30;
    [self.view addSubview:root];
    
    
    self.dataArray = @[
        @{@"top":@5,@"right":@((kScreenWidth - 30)/2.0 - 4.0),@"value":@0.6251,@"image":@1,@"name":@"脑神经系统*"},
        @{@"top":@-19,@"left":@((kScreenWidth - 30)/2.0 - 4.0),@"value":@0.6251,@"image":@7,@"name":@"感官系统*"},
        @{@"top":@12,@"right":@((kScreenWidth - 30)/2.0 - 4.0),@"value":@0.6251,@"image":@2,@"name":@"内分泌系统*"},
        @{@"top":@-17,@"left":@((kScreenWidth - 30)/2.0 - 4.0),@"value":@0.4703,@"image":@8,@"name":@"淋巴系统*"},
        @{@"top":@-17,@"right":@((kScreenWidth - 30)/2.0 - 4.0),@"value":@0.6251,@"image":@3,@"name":@"呼吸系统*"},
        @{@"top":@-10,@"left":@((kScreenWidth - 30)/2.0 - 4.0 + 23.0),@"value":@0.6251,@"image":@9,@"name":@"心血管系统*"},
        @{@"top":@2,@"right":@((kScreenWidth - 30)/2.0 - 4.0),@"value":@0.5531,@"image":@4,@"name":@"消化系统*"},
        @{@"top":@1,@"left":@((kScreenWidth - 30)/2.0 - 4.0 ),@"value":@0.6251,@"image":@10,@"name":@"泌尿系统*"},
        @{@"top":@2,@"right":@((kScreenWidth - 30)/2.0 - 4.0),@"value":@0.6251,@"image":@5,@"name":@"生殖系统*"},
        @{@"top":@4,@"left":@((kScreenWidth - 30)/2.0 - 4.0 + 43.0 ),@"value":@0.5361,@"image":@11,@"name":@"肌肉系统*"},
        @{@"top":@58,@"left":@((kScreenWidth - 30)/2.0 - 4.0 + 41.0 ),@"value":@0.6251,@"image":@12,@"name":@"骨骼系统*"},
        @{@"top":@1,@"right":@((kScreenWidth - 30)/2.0 - 4.0 + 54.0 ),@"value":@0.6251,@"image":@6,@"name":@"染色体*"},
    ];
    for (int i = 0; i < self.dataArray.count; i++) {
        NSDictionary *dic = self.dataArray[i];
        
        NSInteger top = [dic[@"top"] integerValue];
        CGFloat value = [dic[@"value"] floatValue];
        UIColor *color = value > 0.6 ? THEME_COLOR : THEME_SEDCOLOR;
        NSString *imageStr = value > 0.6 ? FormatString(@"organ_%d",[dic[@"image"] intValue])
        : FormatString(@"organ_%d_un",[dic[@"image"] intValue]);
        
        if([[dic allKeys]containsObject:@"right"]){
            NSInteger right = [dic[@"right"] integerValue];
            
            [self addLeftViewWithSuper: root top:top right:right color:color progress:value imageStr:imageStr title:dic[@"name"] tag:i action:@selector(itemClick:)];
        }else{
            NSInteger left = [dic[@"left"] integerValue];
            [self addRightViewWithSuper:root top:top left:left color:color progress:value imageStr:imageStr title:dic[@"name"] tag:i action:@selector(itemClick:)];
        }
    }
    
}

- (void)itemClick:(UIView *)view {
    
    NSInteger tag = view.tag - 100;
    NSDictionary *dic = self.dataArray[tag];
    CGFloat value = [dic[@"value"] floatValue];
    UIColor *color = value > 0.6 ? THEME_COLOR : THEME_SEDCOLOR;
    NSString *imageStr = value > 0.6 ? FormatString(@"organ_%d",[dic[@"image"] intValue])
    : FormatString(@"organ_%d_un",[dic[@"image"] intValue]);
    
    
    OrganDetailView *resultView = [[OrganDetailView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 380)];
    resultView.imageStr = imageStr;
    resultView.color = color;
    resultView.name = dic[@"name"];
    WeakSelf
    resultView.itemClick = ^(NSInteger tag) {
        [weakSelf.zh_popupController dismiss];
        OrganDetailSingleViewController *vc = OrganDetailSingleViewController.new;
        [weakSelf showViewController:vc sender:weakSelf];
    };
    self.zh_popupController = [zhPopupController new];
    self.zh_popupController.layoutType = zhPopupLayoutTypeBottom;
    self.zh_popupController.allowPan = NO;
    self.zh_popupController.maskAlpha = 0.7;
    [self.zh_popupController presentContentView:resultView];
}

- (void)addLeftViewWithSuper:(UIView *)superView top:(CGFloat)top right:(CGFloat)right color:(UIColor *)color progress:(CGFloat)progress imageStr:(NSString *)imageStr title:(NSString *)title  tag:(NSInteger)tag action:(SEL)action{
    
    MyLinearLayout *item = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
    item.myLeft = 0;
    item.myRight = right;
    item.myTop = top;
    item.myHeight = 40;
    item.tag = 100 + tag;
    [superView addSubview:item];
    [item setTarget:self action:action];
    
    MyLinearLayout *content = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
    content.myHorzMargin = 0;
    content.subviewHSpace = 5;
    content.myHeight = 32;
    [item addSubview:content];
    
    {
        YDCircleProgressView * progressView = [[YDCircleProgressView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
        progressView.mySize = CGSizeMake(32, 32);
        progressView.circleRadius = 14;
        progressView.circleBorderWidth = 4;
        progressView.circleColor = [color colorWithAlphaComponent:0.3];
        progressView.progressColor = color;
        progressView.pointRadius = 0;
        [content addSubview:progressView];
        progressView.curProgress = progress;
        
        UIImageView *imageView = [[UIImageView alloc]initWithImage:UIImageMake(imageStr)];
        imageView.frame = CGRectMake(4, 4, 24, 24);
        [progressView addSubview:imageView];
        
        MyLinearLayout *rightLay = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
        rightLay.weight = 1;
        rightLay.myHeight = 32;
        rightLay.subviewVSpace = 2;
        [content addSubview:rightLay];
        
        {
            UILabel *name = [UILabel createLabelText:title font:UIFontMake(12) textColor:COLOR1515];
            name.mySize = kAutoSize;
            [rightLay addSubview:name];
            
            UILabel *value = [UILabel createLabelText:FormatString(@"%.2f%%",progress*100) font:UIFontMake(12) textColor:color];
            value.mySize = kAutoSize;
            [rightLay addSubview:value];
        }
    }
    
    MyLinearLayout *lineLay = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
    lineLay.myHorzMargin = 0;
    lineLay.gravity = MyGravity_Vert_Center;
    lineLay.myHeight = 8;
    [item addSubview:lineLay];
    
    {
        UIView *leftLine = [UIView new];
        leftLine.weight = 1;
        leftLine.myHeight = 0.5;
        leftLine.backgroundColor = THEME_COLOR;
        [lineLay addSubview:leftLine];
        
        UIView *dian = [UIView new];
        dian.mySize = CGSizeMake(8, 8);
        dian.backgroundColor = THEME_COLOR;
        ViewCorner(dian, 4);
        [lineLay addSubview:dian];
    }
}

- (void)addRightViewWithSuper:(UIView *)superView top:(CGFloat)top left:(CGFloat)left color:(UIColor *)color progress:(CGFloat)progress imageStr:(NSString *)imageStr title:(NSString *)title tag:(NSInteger)tag action:(SEL)action{
    MyLinearLayout *item = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
    item.myLeft = left;
    item.myRight = 0;
    item.myTop = top;
    item.myHeight = 40;
    item.tag = 100 + tag;
    [superView addSubview:item];
    [item setTarget:self action:action];
    
    MyLinearLayout *content = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
    content.myHorzMargin = 0;
    content.subviewHSpace = 5;
    content.myHeight = 32;
    [item addSubview:content];
    
    {
        
        UIView *view = UIView.new;
        view.weight = 1;
        [content addSubview:view];
        
        YDCircleProgressView * progressView = [[YDCircleProgressView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
        progressView.mySize = CGSizeMake(32, 32);
        progressView.circleRadius = 14;
        progressView.circleBorderWidth = 4;
        progressView.circleColor = [color colorWithAlphaComponent:0.3];
        progressView.progressColor = color;
        progressView.pointRadius = 0;
        [content addSubview:progressView];
        progressView.curProgress = progress;
        
        UIImageView *imageView = [[UIImageView alloc]initWithImage:UIImageMake(imageStr)];
        imageView.frame = CGRectMake(4, 4, 24, 24);
        [progressView addSubview:imageView];
        
        MyLinearLayout *rightLay = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
        rightLay.myWidth = kAutoWidth;
        rightLay.myHeight = 32;
        rightLay.subviewVSpace = 2;
        [content addSubview:rightLay];
        
        {
            UILabel *name = [UILabel createLabelText:title font:UIFontMake(12) textColor:COLOR1515];
            name.mySize = kAutoSize;
            [rightLay addSubview:name];
            
            UILabel *value = [UILabel createLabelText:FormatString(@"%.2f%%",progress*100) font:UIFontMake(12) textColor:color];
            value.mySize = kAutoSize;
            [rightLay addSubview:value];
        }
        
        
    }
    
    MyLinearLayout *lineLay = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
    lineLay.myHorzMargin = 0;
    lineLay.gravity = MyGravity_Vert_Center;
    lineLay.myHeight = 8;
    [item addSubview:lineLay];
    
    {
        UIView *dian = [UIView new];
        dian.mySize = CGSizeMake(8, 8);
        dian.backgroundColor = THEME_COLOR;
        ViewCorner(dian, 4);
        [lineLay addSubview:dian];
        
        UIView *leftLine = [UIView new];
        leftLine.weight = 1;
        leftLine.myHeight = 0.5;
        leftLine.backgroundColor = THEME_COLOR;
        [lineLay addSubview:leftLine];
        
        
    }
}
@end
